package br.ufmg.dees.insane.parser;


import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import br.ufmg.dees.insane.model.femModel.FemModel;
import br.ufmg.dees.insane.model.postp.PostpModel;
import br.ufmg.dees.insane.model.postp.StepPostp;
import br.ufmg.dees.insane.util.IPoint3d;
import br.ufmg.dees.insane.xyplot.xyplot_model.Axis;
import br.ufmg.dees.insane.xyplot.xyplot_model.ISequence;
import br.ufmg.dees.insane.xyplot.xyplot_model.XYPlot;
import br.ufmg.dees.insane.xyplot.xyplot_view.CaptionState;


public class XYPlotPathPostp extends Observable implements Observer {
	
	//This class is observed by InterfaceXYPlot
	private XYPlot XYPlotModel;
//	private FemModel fem; 
//	private EquilibriumPath solution;
	private String nodeLabel = "";
	private String xValueLabel = "";
	private String yValueLabel = "";
	private String graphTitle = "";
	private String xAxisTitle = "";
	private String yAxisTitle = "";
	private String sequenceTitle = "";
	
	/**
	 * The constructor method. <br>
	 * Calls the init() method throwing an exception.
	 */
	public XYPlotPathPostp(FemModel fem){
		//	this.fem = fem;
		//this.femModelToXYPlot();
	}
	
	public XYPlotPathPostp( ){
		
	}
	
	public void initXYPlot(){
		//creating x axis    
		IPoint3d originX = new IPoint3d(0, 0);  
		IPoint3d endX= new IPoint3d(0, 0);
		Axis x = new Axis(this.xAxisTitle,originX,endX);
		x.setMainMark(1);
		
		//creating y axis    
		IPoint3d originY = new IPoint3d(0, 0); 
		IPoint3d endY= new IPoint3d(0, 0);  
		Axis y = new Axis(this.yAxisTitle,originY,endY);
		y.setMainMark(1);
		
		//creating the axis list 
		List<Axis> axisList = new ArrayList<Axis>();
		axisList.add(x);
		axisList.add(y);
		
		List<IPoint3d> pointsListOne = new ArrayList<IPoint3d>();
		
		IPoint3d pt = new IPoint3d(0,0);
		pointsListOne.add(pt);
		
		//definindo limites superior e inferior da sequencia
		IPoint3d inf1 = new IPoint3d(0.0,0.0);
		IPoint3d sup1= new IPoint3d(10.0,10.0);
		
		//creating a sequence
		ISequence sequenceOne = 
			new ISequence(this.sequenceTitle,pointsListOne, inf1,sup1);
		
		//fill the sequence list
		List<ISequence> sequenceList = new ArrayList<ISequence>();
		sequenceList.add(sequenceOne);
		
		//creating the graph
		XYPlotModel = new XYPlot(this.graphTitle, axisList, sequenceList);
		CaptionState state = new CaptionState();
		state.setXPosition(endX.x);
		state.setYPosition(endY.y);
		
		
	}
	
	/*
	 * Fazer esta classe observar FemModelToPostpModel...
	 * ...Implementar LOGO...
	 */
	public void update(Observable arg0, Object arg) {
		//this.solution = (EquilibriumPath)arg0;
		FemModelToPostpModel parserPostpModel = (FemModelToPostpModel)arg0;
		PostpModel postpModelMesh = new PostpModel();
		postpModelMesh.addStep(parserPostpModel.getStepPostp());
		
		double yp = 0.0;
		double xp = 0.0;
		
		if(yValueLabel=="FinalLoadFactor"){
			yp = (Double)((StepPostp)postpModelMesh.getStepList().get(0)).getStepValue("FinalLoadFactor");
		}else{
			if(((StepPostp)postpModelMesh.getStepList().get(0)).getGeoPostpModelList().get(0)
					.getPointModel(nodeLabel).getValues().containsKey(yValueLabel)){
				yp = (Double)((StepPostp)postpModelMesh.getStepList().get(0)).getGeoPostpModelList().get(0)
				.getPointModel(nodeLabel).getValue(yValueLabel);
			}else{
				yp = 0.0;
			}
		}
		
		if(xValueLabel=="FinalLoadFactor"){
			xp = (Double)((StepPostp)postpModelMesh.getStepList().get(0)).getStepValue("FinalLoadFactor");
		}else{
			if(((StepPostp)postpModelMesh.getStepList().get(0)).getGeoPostpModelList().get(0)
					.getPointModel(nodeLabel).getValues().containsKey(xValueLabel)){
				xp = (Double)((StepPostp)postpModelMesh.getStepList().get(0)).getGeoPostpModelList().get(0)
				.getPointModel(nodeLabel).getValue(xValueLabel);
			}else{
				xp = 0.0;
			}
		}
		
		if(this.XYPlotModel.getAxis(0).getP2().x<(xp)){
			this.XYPlotModel.getAxis(0).setP2(new IPoint3d(1.1*xp, 0));
		}
		if(this.XYPlotModel.getAxis(0).getP1().x>(xp)){
			this.XYPlotModel.getAxis(0).setP1(new IPoint3d(1.1*xp, 0));
		}
		
		if(this.XYPlotModel.getAxis(1).getP2().y<(yp)){
			this.XYPlotModel.getAxis(1).setP2(new IPoint3d(0, 1.1*yp));
		}
		if(this.XYPlotModel.getAxis(1).getP1().y>(yp)){
			this.XYPlotModel.getAxis(1).setP1(new IPoint3d(0, 1.1*yp));
		}
		
		this.XYPlotModel.getSequence(0).getPointsList().add(new IPoint3d(xp,yp));
		
		CaptionState state = new CaptionState();
		state.setXPosition(xp);
		state.setYPosition(yp);
		
		this.update();
	}
	
	public void update() {
		this.setChanged();
		this.hasChanged();
		this.notifyObservers();
	}
	
	/**
	 * @return Returns the xYPlotModel.
	 */
	public XYPlot getXYPlotModel() {
		return XYPlotModel;
	}
	
	/**
	 * @param plotModel The xYPlotModel to set.
	 */
	public void setXYPlotModel(XYPlot plotModel) {
		XYPlotModel = plotModel;
	}
	
	public String getNodeLabel() {
		return nodeLabel;
	}
	
	public void setNodeLabel(String nodeLabel) {
		this.nodeLabel = nodeLabel;
	}
	
	public String getXValueLabel() {
		return xValueLabel;
	}
	
	public void setXValueLabel(String valueLabel) {
		xValueLabel = valueLabel;
	}
	
	public String getYValueLabel() {
		return yValueLabel;
	}
	
	public void setYValueLabel(String valueLabel) {
		yValueLabel = valueLabel;
	}

	public String getGraphTitle() {
		return graphTitle;
	}

	public void setGraphTitle(String graphTitle) {
		this.graphTitle = graphTitle;
	}

	public String getXAxisTitle() {
		return xAxisTitle;
	}

	public void setXAxisTitle(String axisTitle) {
		xAxisTitle = axisTitle;
	}

	public String getYAxisTitle() {
		return yAxisTitle;
	}

	public void setYAxisTitle(String axisTitle) {
		yAxisTitle = axisTitle;
	}

	public String getSequenceTitle() {
		return sequenceTitle;
	}

	public void setSequenceTitle(String seqTitle) {
		this.sequenceTitle = seqTitle;
	}
	
//	*****************************************************************************
	
}